import { trimAll } from 'assets/js/dom'
import { NO_DATA_TEXT, servicePhone } from 'api/config'
import { getThumbPic } from 'assets/js/app'

export const validatorMixin = {
  data() {
    return {
      defaultRules: {
        input: { toast: config => `请输入${config || '内容'}` },
        select: { toast: config => `请选择${config}` },
        number: {
          toast: config => `请输入${config}`,
          lenToast: config => `请输入${config}位数字`
        },
        idno: {
          toast: '请输入身份证号码',
          reg: '^[0-9]{17}([0-9]|[x|X]{1})$',
          regToast: '身份证号码不正确'
        },
        email: {
          toast: '请输入邮箱',
          reg: "^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$",
          regToast: '邮箱不正确'
        },
        tel: {
          toast: config => `请输入${config || '手机号'}`,
          reg: '^1\\d{10}$',
          regToast: config => `${config || '手机号'}不正确`
        }
      }
    }
  },
  methods: {
    validate: function(rules) {
      const items = rules ? (_.isArray(rules) ? rules : [rules]) : Object.keys(rules)
      let postFlag = true
      for (var i = 0; i < items.length; i++) {
        const item = items[i]
        const fieldValue = Object.getValue(this, item.field.trim())
        const value = _.isString(fieldValue) ? trimAll(fieldValue) : fieldValue
        const type = item.type ? item.type : 'input'
        const required = item.required === undefined || item.required
        const ruleType = this.defaultRules[type]

        if (item && required && !value) {
          Toast(
            _.isFunction(ruleType.toast)
              ? ruleType.toast(item.text)
              : ruleType.toast
          )
          postFlag = false
          break
        } else if (item.type === 'number') {
          if (item.len) {
            if (!new RegExp('[0-9]{' + item.len + '}', 'i').test(value)) {
              Toast(ruleType.lenToast(item.len))
              postFlag = false
              break
            }
          }
        } else if (value && item && ruleType.reg) {
          if (!new RegExp(ruleType.reg, 'i').test(value)) {
            Toast(
              _.isFunction(ruleType.regToast)
                ? ruleType.regToast(item.text ? item.text : '')
                : ruleType.regToast
            )
            postFlag = false
            break
          }
        }
      }
      this.$emit('valid', postFlag)
      return postFlag
    }
  }
}

export const baseMixin = {
  methods: {
    getParams(target, fields) {
      if (fields) {
        if (fields.IsString) {
          fields = fields.split(',')
        }
        fields.forEach(key => {
          target[key] = this.$route.params[key] || this.$route.query[key]
        })
      } else {
        Object.keys(this.$route.params).forEach(key => {
          target[key] = this.$route.params[key]
        })
        Object.keys(this.$route.query).forEach(key => {
          target[key] = this.$route.query[key]
        })
      }
      return target
    }
  }
}